package com.wuyi.tea.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wuyi.tea.domain.dto.ArticleDTO;
import com.wuyi.tea.domain.dto.CommentDTO;
import com.wuyi.tea.domain.pojo.Comment;
import com.wuyi.tea.domain.pojo.Picture;
import com.wuyi.tea.domain.pojo.User;
import com.wuyi.tea.domain.vo.CommentVO;
import com.wuyi.tea.domain.vo.UserVO;
import com.wuyi.tea.enums.ConstantEnum;
import com.wuyi.tea.mapper.CommentMapper;
import com.wuyi.tea.mapper.PictureMapper;
import com.wuyi.tea.mapper.UserMapper;
import com.wuyi.tea.service.CommentService;
import com.wuyi.tea.util.LocalStorageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PictureMapper pictureMapper;


    @Override
    public List<Comment> listById(Long id) {
        return commentMapper.listById(id);
    }

    @Override
    public void likeComment(Long commentId) {

        Comment comment = commentMapper.selectById(commentId);
        comment.setLikeNumber(comment.getLikeNumber() + 1);
        commentMapper.updateById(comment);
    }

    @Override
    public List<CommentVO> getProductCommentList(Long productId) {

        List<CommentVO>res = new ArrayList<>();

        // 1.根据商品id获取所有评论
        List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getOrient, ConstantEnum.ORIENT_PRODUCT)
                .eq(Comment::getOrientId, productId)
        );

        // 2.根据评论id获取用户基本信息
        for (Comment comment : comments) {

            CommentVO commentVO = new CommentVO();
            BeanUtils.copyProperties(comment,commentVO);

            User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                    .eq(User::getId, comment.getUserId())
                );

            UserVO userVO = BeanUtil.copyProperties(user,UserVO.class);
            commentVO.setUser(userVO);

            // 3. 根据评论id获取所有图片url
            List<Picture> pictures = pictureMapper.selectList(new LambdaQueryWrapper<Picture>()
                    .eq(Picture::getOrient, ConstantEnum.ORIENT_COMMENT)
                    .eq(Picture::getOrientId, comment.getId())
            );

            List<String>urls=new ArrayList<>();
            pictures.stream().forEach((p) -> urls.add(p.getPicture()));
            commentVO.setPictures(urls);

            res.add(commentVO);
        }

        return res;
    }

    @Transactional
    @Override
    public void releaseComment(Long productId, CommentDTO commentDTO) {

        // 1.设置用户id与商品id
        Long userId = Long.valueOf(LocalStorageUtil.get().toString());
        commentDTO.setUserId(userId);
        commentDTO.setOrient(ConstantEnum.ORIENT_PRODUCT);
        commentDTO.setOrientId(productId);

        // 2.发布评论
        Comment comment = BeanUtil.copyProperties(commentDTO, Comment.class);
        commentMapper.insert(comment);

        // 3.将评论相关的所有图片添加到图片表
        for (String url : commentDTO.getPictures()) {
            Picture picture = new Picture();
            picture.setPicture(url);
            picture.setOrient(commentDTO.getOrient());
            picture.setOrientId(commentDTO.getOrientId());

            pictureMapper.insert(picture);
        }
    }

    @Transactional
    @Override
    public void deleteProductComment(Long commentId) {

        // 1.获取当前用户的id
        Long userId = Long.valueOf(LocalStorageUtil.get().toString());

        // 2.通过评论id查找评论信息
        Comment comment = commentMapper.selectOne(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getId, commentId));

        if (!comment.getUserId().equals(userId)){
            throw new RuntimeException("用户只能删除自己发布的评论");
        }

        // 3.删除该评论相关的所有图片
        pictureMapper.delete(new LambdaQueryWrapper<Picture>()
                .eq(Picture::getOrient,ConstantEnum.ORIENT_COMMENT)
                .eq(Picture::getOrientId,commentId)
        );

        //4.删除当前评论
        commentMapper.deleteById(commentId);
    }

    @Override
    public void releaseArticleComment(Long articleId,CommentDTO commentDTO) {
        Comment comment = BeanUtil.copyProperties(commentDTO,Comment.class);
        comment.setOrient(ConstantEnum.ORIENT_ARTICLE);
        comment.setUserId(Long.valueOf(LocalStorageUtil.get().toString()));
        comment.setLikeNumber(0);
        comment.setDate(new Date());
        comment.setOrientId(articleId);
        commentMapper.insert(comment);
    }

    @Override
    public void addlikeArticleComment(Long articleId) {
        Comment comment = commentMapper.selectById(articleId);
        comment.setLikeNumber(comment.getLikeNumber() + 1);
        commentMapper.updateById(comment);
    }

    @Override
    public List<CommentVO> getArticleAllCommentList(Long articleId) {
        List<Comment> commentList = commentMapper.selectList(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getOrientId, articleId));
        List<CommentVO> commentVOList=new ArrayList<>();

        for (Comment comment : commentList) {

            CommentVO commentVO = BeanUtil.copyProperties(comment,CommentVO.class);
            //user
            User user = userMapper.selectById(comment.getUserId());
            UserVO userVO=BeanUtil.copyProperties(user,UserVO.class);
            commentVO.setUser(userVO);
            //
            commentVOList.add(commentVO);
        }
        return commentVOList;
    }

    @Transactional
    @Override
    public void deleteArticleComment(Long commentId) {
        pictureMapper.delete(new LambdaQueryWrapper<Picture>()
                .eq(Picture::getOrientId,commentId)
        );
        commentMapper.delete(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getId,commentId)
        );
    }
}
